package iii.myangeloldman;

import java.io.File;

import packet.ReportBack;
import packet.ReportBack2;

import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.Toast;

public class Eatmed2 extends Activity {
	MediaPlayer mp;
	String path = "";
	String PicPath = "";
	Boolean change=false;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.eatmed2);
		InitComp();
		
		SharedPreferences table1 = getSharedPreferences("set1", 0);
		String fName = "";
		fName = table1.getString("picF", "");
		path = Environment.getExternalStoragePublicDirectory(
				Environment.DIRECTORY_PICTURES).toString()
				+ "/" + table1.getString("soundF", "");
		PicPath=Environment.getExternalStoragePublicDirectory(
				Environment.DIRECTORY_PICTURES).toString()
				+ "/" + table1.getString("picF", "");
		
		Thread t = new Thread(new Runnable() {

			@Override
			public void run() {
				if (mp == null)
					mp = new MediaPlayer();
				try {
					mp.reset();
					mp.setDataSource(path);
					mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
					mp.prepare();
					mp.start();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
		try {
			File imageDir = Environment
					.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
			Uri uri = Uri.fromFile(new File(imageDir, fName));
			Bitmap bitmap = BitmapFactory.decodeFile(PicPath);
			imgbtn2.setImageBitmap(bitmap);
			
			String[] path1 = { PicPath };
			callMediaScanner(path1);
//			Toast.makeText(Eatmed2.this, PicPath,
//					Toast.LENGTH_LONG).show();
		} catch (Exception e) {
			e.printStackTrace();
		}
		t.start();
		

		//最後執行
//		for (int i = 0; i < 60*5; i++) {
//			try {
//				Thread.sleep(1000);
//				if(change){
//					break;
//				}
//			} catch (InterruptedException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//		}
//		if(!change){
//			SharedPreferences table11 = getSharedPreferences("set1", 0);
//			ReportBack2 r=new ReportBack2();
//			r.oldid=1;
//			r.medid=table11.getInt("Medid", 000);
//			MainActivity.lcl.send(r);
//			
//		Intent go=new Intent(Eatmed2.this,MainActivity.class);
//		startActivity(go);
//		
//		}
	}

	private void callMediaScanner(String[] paths) {
		MediaScannerConnection.scanFile(this, paths, null,
				new MediaScannerConnection.OnScanCompletedListener() {
					public void onScanCompleted(String path, Uri uri) {
						Log.i("ExternalStorageEx", "Scanned " + path + ":");
						Log.i("ExternalStorageEx", "-> uri=" + uri);
					}
				});
	}
	
	private void InitComp() {
		imgbtn2 = (ImageButton) findViewById(R.id.imgbtn2);
		imgbtn2.setOnClickListener(btnNext_click);
		
	}
	ImageButton imgbtn2 = null;
	
	OnClickListener btnNext_click = new OnClickListener() {

		private Handler handler = new Handler();
		private boolean isNext = false;
		{
			new Thread(new Runnable() {
				@Override
				public void run() {
					try {
						Thread.sleep(5*60 * 1000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					if (isNext != true) {
						handler.post(new Runnable() {
							@Override
							public void run() {
								XXXX();
							}
						});
					}
				}
			}).start();
		}

		private void XXXX() {
			SharedPreferences table11 = getSharedPreferences("set0", 0);
			ReportBack2 r = new ReportBack2();
			r.oldid = 1;
			r.medid = table11.getInt("Medid", 000);
			MainActivity.lcl.send(r);

			Intent go = new Intent(Eatmed2.this, MainActivity.class);
			startActivity(go);
		}
		
		
		@Override
		public void onClick(View v) {
			isNext = true;
			try{
//			Toast.makeText(Eatmed2.this, "2吃藥了",
//					Toast.LENGTH_SHORT).show();
			
			//這寫用藥回報
			change=!change;
			SharedPreferences table1 = getSharedPreferences("set1", 0);
			ReportBack r=new ReportBack();
			r.oldid=1;
			r.medid=table1.getInt("Medid", 000);
			MainActivity.lcl.send(r);
			
//			Toast.makeText(Eatmed2.this, r.oldid+"**"+r.medid+" to sql", 
//					Toast.LENGTH_SHORT).show();
			
			 Intent it = new Intent(Eatmed2.this,
			 MainActivity.class);
			 startActivity(it);
			}catch(Exception e){
				e.printStackTrace();
			}
		}
	};
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.eatmed2, menu);
		return true;
	}

}
